﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClipperHouse.jQuery.Base;

namespace ClipperHouse.jQuery
{
	/// <summary>
	/// jQuery toggle() effect. See http://docs.jquery.com/Effects/opacity
	/// </summary>
	public class FadeTo : JQueryWithSpeed
	{
		private const string opacityParamName = "Opacity";

		public override string jQueryMethodName
		{
			get { return "fadeTo"; }
		}

		public FadeTo()
		{
			this.Params.Add(opacityParamName, null);	// careful with decimal, can return 0 if unspecified
			this.SpeedIfRequired = "normal";
			this.Speed = this.SpeedIfRequired;
		}

		protected override void OnLoad(EventArgs e)
		{
			if (this.Opacity == null)
			{
				string exceptionMessage = "ClipperHouse.jQuery.FadeTo requires an Opacity parameter.";

				if (!String.IsNullOrEmpty(this.ID))
				{
					exceptionMessage += " [Control ID: \"" + this.ID + "\"]";
				}
				throw new Exception(exceptionMessage);
			}

			if (String.IsNullOrEmpty(this.Speed))
			{
				string exceptionMessage = "ClipperHouse.jQuery.FadeTo requires a Speed parameter.";

				if (!String.IsNullOrEmpty(this.ID))
				{
					exceptionMessage += " [Control ID: \"" + this.ID + "\"]";
				}
				throw new Exception(exceptionMessage);
			}

			base.OnLoad(e);
		}

		/// <summary>
		/// Toggle elements based upon the switch (true shows elements, false hides elements). 
		/// See http://docs.jquery.com/Effects/opacity
		/// Note: using Switch precludes the use of other parameters such as Speed. Those other parameters will be nullfied if Switch is specified.
		/// </summary>
		public decimal? Opacity
		{
			get
			{
				decimal opacityAsDecimal;

				if (Params[opacityParamName] != null && Decimal.TryParse(Params[opacityParamName].ToString(), out opacityAsDecimal))
				{
					return opacityAsDecimal;
				}
				else
				{
					return null;
				}
			}
			set
			{
				decimal opacityAsDecimal;

				if (Decimal.TryParse(value.ToString(), out opacityAsDecimal))
				{
					Params[opacityParamName] = value.ToString();
				}
				else
				{
					Params[opacityParamName] = null;
				}
			}
		}
	}
}
